package com.kfm.am;

import lombok.Data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class Book {

    private Integer id;
    private String bookTitle;
    private Date publicationDate;
    private String author;
    private Double price;
    private Integer quantity;

    public static class BookHandler implements Handler<Book> {
        // 通过解析 ResultSet Book的set方法 每一行数据封装 Book对象  变成List集合
        @Override
        public List<Book> handle(ResultSet set) throws SQLException {
            List<Book> books = new ArrayList<>();
            while (set.next()) {
                // set 代表一行数据
                Book book = new Book();
                book.setId(set.getInt("id"));
                book.setBookTitle(set.getString("book_title"));
                book.setPublicationDate(set.getDate("publication_date"));
                book.setAuthor(set.getString("author"));
                book.setPrice(set.getDouble("price"));
                book.setQuantity(set.getInt("quantity"));
                books.add(book);
            }
            return books;
        }
    }
}
